package cn.schoolwow.quickdao.test.module.business.user.dql.general.testcase;

import cn.schoolwow.quickdao.domain.DAO;
import cn.schoolwow.quickdao.test.entity.Role;
import cn.schoolwow.quickdao.test.entity.User;
import cn.schoolwow.quickdao.test.entity.UserRole;
import cn.schoolwow.quickdao.test.module.business.user.dml.userRole.testcase.InsertUserRoleTestCaseFlow;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import org.junit.Assert;

public class JoinTableJSONFieldQueryTestCaseFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        flowContext.executeFlowList(new InsertUserRoleTestCaseFlow());

        joinTableJsonConditionQuery(flowContext);
    }

    @Override
    public String name() {
        return "JSON字段类型返回";
    }

    private void joinTableJsonConditionQuery(FlowContext flowContext){
        DAO dao = flowContext.checkInstanceData(DAO.class);

        User user = (User) dao.query(UserRole.class)
                .joinTable(User.class, "roleId", "id")
                .addJSONQuery("extraData", "age", "=","10")
                .addTableColumn()
                .done()
                .joinTable(Role.class, "roleId", "id")
                .addQuery("roleKey", "admin")
                .done()
                .execute()
                .getOne(User.class);
        Assert.assertTrue(user.getExtraData().containsKey("age"));
        Assert.assertEquals(10, user.getExtraData().getIntValue("age"));
    }
}
